package com.orkasgb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orkasgb.entity.OrkasgbTaskDetailsDataEntity;
import com.orkasgb.entity.OrkasgbTaskDetailsEntity;
import com.orkasgb.entity.OrkasgbTaskEntity;
import com.orkasgb.mapper.OrkasgbTaskMapper;
import com.orkasgb.service.IOrkasgbTaskDetailsService;
import com.orkasgb.service.IOrkasgbTaskService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 任务信息表(TOrkasgbTask)表服务实现类
 *
 * @version 1.0.0
 * @since 2024-09-08 21:17:23
 */
@Slf4j
@Service
public class OrkasgbTaskServiceImpl extends ServiceImpl<OrkasgbTaskMapper, OrkasgbTaskEntity> implements IOrkasgbTaskService {
	
	@Resource
	private OrkasgbTaskMapper orkasgbTaskMapper;
	
	@Resource
	private IOrkasgbTaskDetailsService orkasgbTaskDetailsService;
	
	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@Override
	public OrkasgbTaskEntity getOrkasgbTask (Serializable id) {
		return this.getBaseMapper().selectById(id);
	}
	
	/**
	 * 通过条件查询多条数据
	 *
	 * @param orkasgbTaskEntity 实体查询参数
	 * @return 数据
	 */
	@Override
	public List<OrkasgbTaskEntity> getOrkasgbTaskList (OrkasgbTaskEntity orkasgbTaskEntity) {
		QueryWrapper<OrkasgbTaskEntity> queryWrapper = new QueryWrapper<>(OrkasgbTaskEntity.class);
		return this.getBaseMapper().selectList(queryWrapper);
	}
	
	/**
	 * 通过条件查询多条数据
	 *
	 * @param orkasgbTaskDetailsData 实体查询参数
	 * @return 数据
	 */
	@Override
	public IPage<OrkasgbTaskDetailsDataEntity> getOrkasgbTaskDetailsDataList (OrkasgbTaskDetailsDataEntity orkasgbTaskDetailsData) {
		IPage<OrkasgbTaskDetailsDataEntity> page = new Page<>(orkasgbTaskDetailsData.getCurrent(), orkasgbTaskDetailsData.getPageSize());
		page.setRecords(orkasgbTaskMapper.getOrkasgbTaskDetailsDataList(orkasgbTaskDetailsData));
		page.setTotal(orkasgbTaskMapper.getOrkasgbTaskDetailsDataListCount(orkasgbTaskDetailsData));
		page.setPages(page.getPages());
		page.setCurrent(orkasgbTaskDetailsData.getCurrent());
		page.setSize(orkasgbTaskDetailsData.getPageSize());
		return page;
	}
	
	/**
	 * 通过条件查询多条数据(分页查询)
	 *
	 * @param orkasgbTaskEntity 查询参数实体
	 * @return 数据
	 */
	@Override
	public List<OrkasgbTaskEntity> getOrkasgbTaskPageDataList (OrkasgbTaskEntity orkasgbTaskEntity) {
		QueryWrapper<OrkasgbTaskEntity> queryWrapper = new QueryWrapper<>(OrkasgbTaskEntity.class);
		IPage<OrkasgbTaskEntity> page = new Page<>(1, 10);
		return this.getBaseMapper().selectList(page, queryWrapper);
	}
	
	/**
	 * 新增数据
	 *
	 * @param orkasgbTaskDetails 实体对象
	 * @return 新增结果
	 */
	@Override
	public int addOrkasgbTask (OrkasgbTaskDetailsDataEntity orkasgbTaskDetails) {
		OrkasgbTaskEntity orkasgbTaskEntity = new OrkasgbTaskEntity();
		BeanUtils.copyProperties(orkasgbTaskDetails, orkasgbTaskEntity);
		this.getBaseMapper().insert(orkasgbTaskEntity);
		
		OrkasgbTaskDetailsEntity orkasgbTaskDetailsEntity = new OrkasgbTaskDetailsEntity();
		BeanUtils.copyProperties(orkasgbTaskDetails, orkasgbTaskDetailsEntity);
		orkasgbTaskDetailsEntity.setTaskId(orkasgbTaskEntity.getId());
		orkasgbTaskDetailsEntity.setTaskStatus("1");
		orkasgbTaskDetailsService.addOrkasgbTaskDetails(orkasgbTaskDetailsEntity);
		return 1;
	}
	
	/**
	 * 修改数据
	 *
	 * @param orkasgbTaskDetails 实体对象
	 * @return 修改结果
	 */
	@Override
	public int updateOrkasgbTask (OrkasgbTaskDetailsDataEntity orkasgbTaskDetails) {
		OrkasgbTaskDetailsEntity orkasgbTaskDetailsEntity = new OrkasgbTaskDetailsEntity();
		BeanUtils.copyProperties(orkasgbTaskDetails, orkasgbTaskDetailsEntity);
		orkasgbTaskDetailsEntity.setTaskId(orkasgbTaskDetails.getId());
		orkasgbTaskDetailsService.updateById(orkasgbTaskDetailsEntity);
		
		OrkasgbTaskEntity orkasgbTaskEntity = new OrkasgbTaskEntity();
		BeanUtils.copyProperties(orkasgbTaskDetails, orkasgbTaskEntity);
		return this.getBaseMapper().updateById(orkasgbTaskEntity);
	}
	
	/**
	 * 修改或者新增数据
	 *
	 * @param orkasgbTaskEntityList 实体对象
	 * @return 操作结果
	 */
	@Override
	public List<BatchResult> addOrUpdateOrkasgbTask (List<OrkasgbTaskEntity> orkasgbTaskEntityList) {
		return orkasgbTaskMapper.insertOrUpdate(orkasgbTaskEntityList);
	}
	
	/**
	 * 删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 */
	@Override
	public int deleteOrkasgbTaskByIds (List<Long> idList) {
		orkasgbTaskDetailsService.remove(new LambdaQueryWrapper<OrkasgbTaskDetailsEntity>()
				.in(OrkasgbTaskDetailsEntity::getTaskId, idList));
		return this.getBaseMapper().deleteByIds(idList);
	}
	
}

